function stopDefault(e){
    var e=e||window.event;
    if(e.stopPropagation){
        e.stopPropagation();
        e.preventDefault()
    }else{
        e.cancelBubble=true;
        e.returnValue=false;
    }
}
//下拉
$(function(){
    var $label=jQuery('.select .label'),
        $optionItem=jQuery('.select ul.option li');
    $label.each(function(){
      jQuery(this).on('touchstart',function(e){
            var e=e||window.event;
            if(e.stopPropagation){
                e.stopPropagation()
            }else{
                e.cancelBubble=true
            }
            var index= $label.index(jQuery(this));
        jQuery(this).stop().toggleClass('selected').siblings('.option').slideToggle();
            for(var i=0,max=$label.length;i<max;i++){
                if(index!=i)$label.eq(i).stop().removeClass('selected').siblings('.option').slideUp();
            }
        });
    });
    $optionItem.on('touchstart',function(){
        var text=jQuery(this).text();
      jQuery(this).parent().siblings('p.label').text(text)
    })
  jQuery(document).on('touchstart',function(){
        $label.stop().removeClass('selected').siblings('.option').slideUp();
    });
});

//tab
jQuery(function(){
    var $panel=jQuery('.tabPanel .panel');
    $panel.on('touchstart',function(){
        var index=jQuery(this).parent().children('.panel').index(this);
      jQuery(this).parent().siblings('.tabContent').children('.tabItem').eq(index).show().siblings().hide();
      jQuery(this).addClass('cur').siblings('.panel').removeClass('cur')
    });
});

//效果封装

(function($){
//   touchFade
    /*
    * 参数配置
    * index: 初始序号位置
    * autoPlay：是否自动播放 默认true
    * playTime 自动播放时间 默认 4000
    * callback 每次执行完成后执行，callback(index) 参数 index,当前index序号值
    * */
    function touchFade(option){
        var index=option.index,
            nextIndex=index;
        var $item=option._this,
            $container=$item.parent(),
            len=$item.length,
            _w=$item.width(),
            isMove=false,
            startPoint=0,
            play=null,
            distance=0;
        var callback=option.callback;
        var playTime=option.playTime||4000;
        var isAuto=option.autoPlay==undefined||option.autoPlay==true?true:false;
        for(var i=0;i<len;i++){
            if(i!=index)$item.eq(i).hide().css({opacity:0,"z-index":1});
            else $item.eq(i).show().css({opacity:1,"z-index":2});
        }
        $item.on("touchstart",function(){
            event.stopPropagation();
        });
        $container.on("touchmove",function(e){
            event.preventDefault();
            if($item.is(':animated'))return;
            clearInterval(play)
            play=null;
            if(!isMove){
                startPoint=e.touches[0].pageX;
                isMove=true;
            }
            distance=e.touches[0].pageX-startPoint;
            nextIndex=distance>0?index-1:index+1;
            nextIndex=nextIndex>=len?0:nextIndex;
            nextIndex=nextIndex<0?len-1:nextIndex;
            $item.eq(index).css({opacity:1-Math.abs(distance/_w)});
            $item.eq(nextIndex).show(0).css({opacity:Math.abs(distance/_w)});
        });
        function toNext(){
            var time=(1-Math.abs(distance/_w))*400;
            $item.eq(index).animate({'z-index':1,opacity:0},time,function(){
                $item.eq(index).hide(0);
                index=nextIndex;
                distance=0;
                if(callback){
                    callback(index)
                }
            });
            $item.eq(nextIndex).animate({'z-index':2,opacity:1},time);
        }
        function reduction(){
            var time=(1-Math.abs(distance/_w))*400;
            $item.eq(nextIndex).animate({'z-index':1,opacity:0},time,function(){
                $item.eq(nextIndex).hide(0);
                nextIndex=index;
                distance=0;
                if(callback){
                    callback(index)
                }
            });
            $item.eq(index).animate({'z-index':2,opacity:1},time);
        }
        $container.on('touchend',function(){
            isMove=false;
            if(nextIndex==index)return;
            Math.abs(distance)>_w/5?toNext():reduction();
            if(!isAuto)return;
            play=setInterval(function(){
                nextIndex++;
                nextIndex=nextIndex>=len?0:nextIndex;
                autoPlay()
            },playTime)
        });
        function autoPlay(){
            $item.eq(index).animate({'z-index':1,opacity:0},400,function(){
                $item.eq(index).hide(0);
                index=nextIndex;
                distance=0;
                if(callback){
                    callback(index)
                }
            });
            $item.eq(nextIndex).show(0).animate({'z-index':2,opacity:1},400);

        }
        if(!isAuto)return;
        play=setInterval(function(){
            nextIndex=index+1;
            nextIndex=nextIndex>=len?0:nextIndex;
            autoPlay()
        },playTime)
    }

    /*
    * 参数说明
    * space 间距，值越大间距越小 默认20
    * index 初始位置 默认 0
    * coefficient 系数 系数越大 间距越大，默认 50
    * time 过渡时间 默认200
    * autoPlay 是否自动播放
    * playTime 自动播放时间 默认 4000
    * callback
    * */
    function touchSwitch(option){
        var $item=option._this,
            $container=$item.parent(),
            len=$item.length,
            croods=[];
        var distance=0,
            isMove=false,
            startP=0,
            index=option.index||0,next=0,per=1;
        var coefficient=option.coefficient;
        var _w=Math.ceil($item.width())-option.space;
        var time=option.time||200,
            callback=option.callback;
        var isAuto=option.autoPlay==undefined||option.autoPlay==true?true:false,
            play=null,
            playTime=option.playTime||4000;
        init();
        function init(){
            _w=Math.ceil($item.width())-option.space;
            for(var i=0;i<len;i++){
                $item.eq(i).css({left:(i-index)*_w});
                croods.push((i-index)*_w)
            };
        }
        $(window).resize(init);
        function bannerMove(nextIndex,per){
            var dis=nextIndex-index;
            $item.css({'transition-duration': parseInt(time*per)+'ms'});
            for(var i =0;i<len;i++){
                $item.eq(i).css({left:croods[i]+-dis*_w});
                croods[i]=croods[i]+-dis*_w;
                if(i==nextIndex){
                    $item.eq(i).css({transform:'translate3d(0px, 0px,0px) rotateX(0deg) rotateY(0deg)'})
                }else{
                    $item.eq(i).css({transform:'translate3d(0px, 0px,-100px) rotateX(0deg) rotateY(-'+coefficient+'deg)'})
                }
            }
            index=nextIndex;
            per=1;
            if(callback){
                setTimeout(callback,time*per)
            }

        };
        $container.on('touchmove',function(e){
            var e=window.event||e;
            stopDefault(e);
            if(isAuto){
                clearInterval(play);
                play=null;
            }
            if($item.is(':animated'))return;
            if(!isMove){
                startP=e.touches[0].pageX;
                isMove=true;
            }
            distance=e.touches[0].pageX-startP;
            distance=parseInt(distance/2);
            $item.css({'transition-duration':'0ms'});
            for(var i=0;i<len;i++){
                var newLeft=croods[i]+distance;
                $item.eq(i).css({left:newLeft})
            }
            per=Math.abs(distance/_w);
            $item.css({'transition-duration':'0ms'});
            $item.eq(index).css({transform:'translate3d(0px, 0px, '+-per*100+'px) rotateX(0deg) rotateY('+-per*coefficient+'deg)'});
            var nextIndex=distance<0?index+1:index-1;
            if(nextIndex!=-1&&nextIndex!=len){
                next=nextIndex;
                $item.eq(nextIndex).css({transform:'translate3d(0px, 0px, '+(per*100-100)+'px) rotateX(0deg) rotateY('+(per*50-coefficient)+'deg)'});
                return
            }
            next=index;
        });
        $container.on('touchend',function(){
            isMove=false;
            Math.abs(distance)>_w/6?bannerMove(next,per):bannerMove(index,per);
            distance=0;
            play=setInterval(autoPlay,playTime);
        });
        function autoPlay(){
            next=index+1;
            next=next>=len?0:next;
            bannerMove(next,per)
        }
        if(isAuto){
            play=setInterval(autoPlay,playTime);
        }
    }

  $.fn.extend({
    touchSwitch:function(option){
      var  option=option ||{};
      option._this=$(this);
      touchSwitch(option)
    },
    touchFade:function(option){
      var  option=option ||{};
      option._this=$(this);
      touchFade(option)
    }
  })
})(jQuery);
